program FairSquareBIGMANjustforfunAuthorSolution;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  math,
  BigInt in 'BigInt.pas',
  BInt in 'BInt.pas';

var
  f, g, ff, gg: TextFile;
  a, b: number;
  answer: Longint;


procedure ReadData;
begin
  readFromFile(f, a, b);
  //Readln(f, a, b);
end;

function IsPalindrome(s: String): Boolean; overload;
var
  i, len: byte;
begin
  len := Length(s);
  if len < 2 then begin
    Result := True;
    Exit;
  end;

  for i := 1 to len shr 1 do
    if s[i] <> s[len + 1 - i] then begin
      Result := False;
      Exit;
    end;
  Result := True;
end;

function IsPalindrome(i: Longint): Boolean; overload;
begin
  Result := IsPalindrome(IntToStr(i));
end;

function IsPalindrome(i: number): Boolean; overload;
begin
  Result := IsPalindrome(toString(i));
end;

procedure MakePalindrome(var a:number; leng: byte);
var
  i: byte;
begin
  for i := leng downto (leng shr 1) + 1 do
    a[leng + 1 - i] := a[i];
end;

function NextPalindrome(var i: number): number;
var
  j, x: number;
  k, leng: byte;
  bv: longint;
begin
  j := copy(i);
  leng := len(j);
  MakePalindrome(j, leng);
  if cmp(j, i) = 1 then begin
    Result := j;
    Exit;
  end;
  repeat
    repeat
      leng := len(j);
      for k := (leng shr 1) + 1 to leng do begin
        bv := BIntVariance(j);
        if (bv{BIntVariance(j)} > 9) then begin
          set0(x);
          x[k] := 10 - j[k];
          add(j, x);
          leng := len(j);
          MakePalindrome(j, leng);
          bv := BIntVariance(j);
          if (bv < 10) then
            Break;
        end else if (j[k] > 1) then begin
          set0(x);
          x[k] := 10 - j[k];
          add(j, x);
          if j[k + 1] < 3 then
            Break;
        end else begin
          set0(x);
          x[k] := 1;
          add(j, x);
          Break;
        end;
      end;
      leng := len(j);
      MakePalindrome(j, leng);
    until ((BIntVariance(j) < 10) or (cmp(j, b) = 1));

  until cmp(j, i) = 1;
  Result := j;
end;

procedure ProcessData;
var
  k, fl, sk: number;
  aa, bb, ce, flo: BigInteger;
  bv: longint;
begin
  aa := BigInteger.Create(a, 1);
  bb := BigInteger.Create(b, 1);
//  writeln('trunc(sqrt(a)) ', trunc(sqrt(a)), ' ceil(sqrt(a)) ', ceil(sqrt(a)), ' trunc(sqrt(b)) ', trunc(sqrt(b)));
  ce := aa;//.SqrtRough;//  aa.SqrtCeil;
  flo := bb;//.SqrtRough;//bb.SqrtFloor;
  k := ce.GetAbsoluteValue;
  fl := flo.GetAbsoluteValue;
  write('ce ');
  show(k);
  write(' fl ');
  show(fl);
  writeln;
  writeln;
  repeat
    if IsPalindrome(Sq(k)) then begin
      Inc(Answer);
      sk := Sq(k);
      write('Square of ');
      show(k);
      write(' is ');
      show(sk);
      writeln(' is FAIR SQUARE!!!!');
      write(gg, 'Square of ');
      showf(gg, k);
      write(gg, ' is ');
      showf(gg, sk);
      writeln(gg, ' is FAIR SQUARE!!!!');
    end;
    write('-- current k ');
    show(k);
    sk := Sq(k);
    write(' and its square is ');
    show(sk);
    bv := BIntVariance(k);
    writeln(' and sum of squares of its digits is ', bv);

    write(ff, 'current k ');
    showf(ff, k);
    sk := Sq(k);
    write(ff, ' and its square is ');
    showf(ff, sk);
    writeln(ff);
    Flush(ff);

    k := NextPalindrome(k);
  until cmp(k, fl) = 1;
  //for i := Low to High do

  //writeln(g, 'Case #', casenum, ': ', verdict);
end;


procedure OutputVerdict(casenum: Word);
begin
  writeln(g, 'Case #', casenum, ': ', answer);
  writeln('Case #', casenum, ': ', answer);
end;

var
  i, t: Byte;
  s: String;
begin
  //writeln(MaxLongInt);
  try
    s := ParamStr(1);
    if (s = '') then
       s := 'inputBAS.txt';
    AssignFile(f, s);
    Reset(f);
    AssignFile(g, 'outputBAS1.txt');
    Rewrite(g);

    AssignFile(ff, 'debugBAS.txt');
    Rewrite(ff);
    AssignFile(gg, 'FirstBAS.txt');
    Rewrite(gg);
    try
      Readln(f, t);
      for i := 1 to t do begin
        answer := 0;
        ReadData;
        show(a);
        writeln;
        show(b);
        writeln;
        ProcessData;
        OutputVerdict(i);
      end;
    finally
      CloseFile(f);
      CloseFile(g);
      //CloseFile(ff);
      CloseFile(gg);
    end;
    writeln('Process finished');
    readln;
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
      readln;
    end;
  end;
end.
